package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.module.admin.entity.SysDoctor;
import com.mars.module.admin.mapper.SysDoctorMapper;
import com.mars.module.admin.service.ISysDictDataService;
import com.mars.module.system.mapper.SysUserMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysReservationRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysReservationMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysReservation;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysReservationService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 患者挂号预约业务层处理
 *
 * @author mars
 * @date 2024-04-12
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysReservationServiceImpl implements ISysReservationService {

    private final SysReservationMapper baseMapper;
    private final SysDoctorMapper doctorMapper;
    private final SysUserMapper userMapper;
    private final ISysDictDataService dataService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysReservation add(SysReservationRequest request) {
        SysReservation entity = SysReservation.builder().build();
        BeanUtils.copyProperties(request, entity);
        Long userId = ContextUserInfoThreadHolder.get().getId();
        entity.setUserId(userId);
        entity.setStatus(0L);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysReservationRequest request) {
        SysReservation entity = SysReservation.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysReservation getById(Long id) {
        SysReservation reservation = baseMapper.selectById(id);
        reservation.setUserName(userMapper.selectById(reservation.getUserId()).getUserName());
        return reservation;
    }

    @Override
    public PageInfo<SysReservation> pageList(SysReservationRequest request) {
        Page<SysReservation> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysReservation> query = this.buildWrapper(request);
        IPage<SysReservation> pageRecord = baseMapper.selectPage(page, query);
        List<SysReservation> records = pageRecord.getRecords();
        List<SysReservation> recordsNew = new ArrayList<>();
        for (SysReservation reservation : records) {
            reservation.setStatusName(dataService.getDictDataById("reservation_type", reservation.getStatus().toString()));
            reservation.setUserName(userMapper.selectById(reservation.getUserId()).getUserName());
            reservation.setDoctorName(doctorMapper.selectById(reservation.getDoctor()).getName());
            recordsNew.add(reservation);
        }
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysReservation> list(SysReservationRequest request) {
        LambdaQueryWrapper<SysReservation> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysReservation> buildWrapper(SysReservationRequest param) {
        LambdaQueryWrapper<SysReservation> query = new LambdaQueryWrapper<>();
        return query;
    }

}
